﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using LeHouse.Server.Dto;
using LeHouse.Server.Service;
using LeHouse.Server.WebApi.Common;
namespace LeHouse.Server.WebApi.Controllers
{
    public class StaffController : ApiController
    {
        /// <summary>
        /// 员工信息的下拉框的获取
        /// </summary>
        /// <param name="Dmenuid">枚举编号</param>
        /// <returns>符合要求的下拉框数据</returns>
        [HttpGet]
        [Route("api/Staff/GetHouseType")]
        public IEnumerable<System_Staff> GetHouseType([FromUri]int deptid)
        {
            //字典表的实例化
            System_StaffService dic = new System_StaffService();
            System_DeptStaffService dsf = new System_DeptStaffService();
            List<System_DeptStaff> sds = dsf.Select(t => t.T_DepartmentID == deptid);
            List<System_Staff> droplist = new List<System_Staff>();
            foreach (var v in sds)
            {
                droplist.AddRange(dic.Select(t => t.T_StaffID == v.T_StaffID));
            }

            return droplist;
        }

        /// <summary>
        /// 员工信息的数据添加
        /// </summary>
        /// <param name="s">ajax传过来的需要添加的数据</param>
        /// <returns></returns>       
        [HttpPost]
        [Route("api/Staff/Postinsert")]
        public bool Insert([FromBody] System_Staff parms)
        {
            parms.T_Date = DateTime.Now;
            parms.T_State = 1;
            parms.T_IsSystem = 1;
            System_StaffService insert = new System_StaffService();
            if (insert.StaffInsertOrUpdate(parms) == true)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">员工的编号</param>
        /// <returns>false/true</returns>
        [HttpPost]
        [Route("api/Staff/Delete")]
        public bool Delete([FromBody] int id)
        {
            System_StaffService delete = new System_StaffService();
            if (delete.StaffDel(id) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据员工编号获取员工的信息
        /// </summary>
        /// <param name="?">员工的编号</param>
        /// <returns>false/true判断是否为空</returns>
        [HttpPost]
        [Route("api/Staff/UpdateByid")]
        public dynamic UpdateBYid([FromBody]int id)
        {
            System_StaffService updatebyid = new System_StaffService();
            System_RoleStaffService role = new System_RoleStaffService();
            System_DeptStaffService dept = new System_DeptStaffService();
            var d1 = updatebyid.SelectById(id);
            d1.T_Department = dept.Select(t => t.T_StaffID == id).Select(t => t.T_DepartmentID).ToArray();
            d1.T_Role = role.Select(t => t.T_StaffID == id).Select(t => t.T_RoleID).ToArray();
            return d1;
        }

        /// <summary>
        /// 修改员工信息
        /// </summary>
        /// <param name="sta">获取修改的员工信息</param>
        /// <returns>false/true</returns>
        [HttpPost]
        [Route("api/Staff/Update")]
        public bool Update([FromBody] System_Staff sta)
        {
            System_StaffService insert = new System_StaffService();
            System_Staff ss = insert.SelectById(sta.T_StaffID);
            if (ss == null)
                return false;
            ss.T_Name = sta.T_Name;
            ss.T_Sex = sta.T_Sex;
            ss.T_IDcard = sta.T_IDcard;
            ss.T_Phone = sta.T_Phone;
            ss.T_Email = sta.T_Email;
            ss.T_Account = sta.T_Account;
            ss.T_Department = sta.T_Department;
            ss.T_Role = sta.T_Role;

            if (insert.StaffInsertOrUpdate(ss) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改员工状态
        /// </summary>
        /// <param name="sta"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Staff/UpdateState")]
        public bool UpdateState(System_Staff obj)
        {
            System_StaffService update = new System_StaffService();
            var d1 = update.SelectById(obj.T_StaffID);
            d1.T_State = obj.T_State;
            if (update.Update(d1) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 根据条件获取员工信息集
        /// </summary>
        /// <param name="S_name"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Staff/Select")]
        public List<System_Staff> Select()
        {
            System_StaffService staff = new System_StaffService();
            var d1 = staff.Select();
            return d1;
        }

        /// <summary>
        /// 根据条件获取员工信息集(分页)
        /// </summary>
        /// <param name="S_name"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Staff/Selectpage")]
        public SelectPager<System_Staff> Selectpage([FromBody]SelectPager<System_Staff> obj)
        {
            SelectPager<System_Staff> sp = new SelectPager<System_Staff>();
            System_StaffService staff = new System_StaffService();
            if (string.IsNullOrEmpty(obj.search))
            {
                long totalSize;
                sp.Result = staff.SelectByPager(obj.PageIndex, obj.PageSize, out totalSize);
                sp.PageCount = Convert.ToInt32(Math.Ceiling((double)totalSize / obj.PageSize));
                sp.TotalSize = Convert.ToInt32(totalSize);
            }
            else
            {
                long totalSize;
                sp.Result = staff.SelectByPager(p => p.T_Name.Contains(obj.search), obj.PageIndex, obj.PageSize, out totalSize);
                sp.PageCount = Convert.ToInt32(Math.Ceiling((double)totalSize / obj.PageSize));
                sp.TotalSize = Convert.ToInt32(totalSize);
            }
            return sp;
        }

        /// <summary>
        /// 获取全部员工信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Staff/selectStaff")]
        public List<System_Staff> selectStaff()
        {
            System_StaffService staff = new System_StaffService();
            var d1 = staff.Select();
            return d1;
        }

    }
}
